package com.easy_helth.controller;
import com.alibaba.fastjson.JSONObject;
import com.easy_helth.entity.WxUser;
import com.easy_helth.service.WxUserService;
import com.easy_helth.util.WeChatUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Slf4j
@Controller
@RequestMapping(value = "/wxuser")
public class WxUserController {

    @Value("${wxMini.appId}")
    public String appId;
    @Value("${wxMini.secret}")
    public String secret;
    @Autowired
    private WxUserService wxUserService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject login(@RequestBody WxUser wxUser){
        String code = wxUser.getCode();
        JSONObject object=new JSONObject();
        if(code == "" || "".equals(code)){
            object.put("code",300);
            object.put("msg","code不能为空!");
            return object;
        }
        else {
            //微信接口服务,通过调用微信接口服务中jscode2session接口获取到openid和session_key
            String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
            String str = WeChatUtil.httpRequest(url, "GET", null); //调用工具类解密
            JSONObject jsonObject=JSONObject.parseObject(str);
            String openid = (String) jsonObject.get("openid");
            if(openid != null && !"".equals(openid)){
                //登录成功
                WxUser wxuserVo=new WxUser();
                wxuserVo.setNickName(wxUser.getNickName());
                wxuserVo.setAvatarUrl(wxUser.getAvatarUrl());
                wxuserVo.setOpenId(openid);
                wxuserVo.setGender(wxUser.getGender());
                wxuserVo.setFirstLoginTime(new Date(System.currentTimeMillis()));
                wxuserVo.setLastLoginTime(new Date(System.currentTimeMillis()));
                WxUser us = wxUserService.getById(openid);
                if(us != null) {
                    //不是首次登录，更新用户信息
                    wxuserVo.setId(us.getId());
                    wxUserService.updateById(wxuserVo);
                }
                else {
                    //首次登录，存储用户信息
                    wxUserService.save(wxuserVo);
                }
                object.put("code",200);
                object.put("msg","登录成功!");
                object.put("wxuserInfo",wxuserVo);
                log.info("登录成功");
                return object;
            }else {
                object.put("code",400);
                object.put("msg","未知错误，请重试!");
                log.info("登录失败");
                return object;
            }
        }

    }
}